package com.huike.web.controller.review;


import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.review.pojo.Review;
import com.huike.review.service.ReviewService;
import com.huike.review.vo.MybatisReviewVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 该Controller主要是为了复习三层架构以及Mybatis使用的，该部分接口已经放开权限，可以直接访问
 * 同学们在此处编写接口通过浏览器访问看是否能完成最简单的增删改查
 */
@RestController
@RequestMapping("/review")
public class MybatisReviewController extends BaseController {

    @Autowired
    private ReviewService reviewService;

    /**
     *功能描述:保存数据
     * @param name
     * @param age
     * @param sex
     *@return:
     */
    @GetMapping("/saveData/{name}/{age}/{sex}")
    public AjaxResult saveData(@PathVariable String name, @PathVariable Integer age, @PathVariable String sex){
        Review review = new Review();
        review.setName(name);
        review.setAge(age);
        review.setSex(sex);
        reviewService.saveData(review);
        return AjaxResult.success("插入成功!");
    }

    /**
     *功能描述:保存json数据
     * @param mybatisReviewVO
     *@return:
     */
    @PostMapping("saveData1")
    public AjaxResult saveData1(@RequestBody MybatisReviewVO mybatisReviewVO){
        Review review = new Review();
        review.setName(mybatisReviewVO.getName());
        review.setAge(mybatisReviewVO.getAge());
        review.setSex(mybatisReviewVO.getSex());
        reviewService.saveData1(review);
        return AjaxResult.success("插入成功!");
    }

    /**
     *功能描述:修改数据
     * @param mybatisReviewVO
     *@return:
     */
    @PostMapping("/update")
    public AjaxResult update(@RequestBody MybatisReviewVO mybatisReviewVO){
        Review review = new Review();
        review.setId(mybatisReviewVO.getId());
        review.setName(mybatisReviewVO.getName());
        review.setAge(mybatisReviewVO.getAge());
        review.setSex(mybatisReviewVO.getSex());
        reviewService.update(review);
        return AjaxResult.success("修改成功!");
    }

    /**
     *功能描述:删除成功
     * @param id
     *@return:
     */
    @DeleteMapping("/remove/{id}")
    public AjaxResult remove(@PathVariable Long id){
        reviewService.remove(id);
        return AjaxResult.success("删除成功!");
    }

    /**
     *功能描述:批量删除
     * @param list
     *@return:
     */
    @DeleteMapping("/bachDeleteByIds")
    public AjaxResult bachDeleteByIds(@RequestBody int[] ids){
        reviewService.bachDeleteByIds(ids);
        return AjaxResult.success("删除成功!");
    }

    /**
     *功能描述:根据id查询
     * @param id
     *@return:
     */
    @GetMapping("/getById")
    public AjaxResult getById(Long id){
        Review review = reviewService.getById(id);
        return AjaxResult.success(review);
    }

    /**
     *功能描述:分页查询
     * @param pageNum
     * @param pageSize
     *@return:
     */
    @GetMapping("getDataByPage")
    public TableDataInfo getDataByPage(Integer pageNum, Integer pageSize){
        startPage();
        List<Review> list = reviewService.getDataByPage();
        List<MybatisReviewVO> lists = list.stream().map((item)->{
            MybatisReviewVO reviewVO = new MybatisReviewVO();
            BeanUtils.copyProperties(item,reviewVO);
            return reviewVO;
        }).collect(Collectors.toList());

        return getDataTablePage(lists) ;
    }




}